package com.usian.behavior.service.impl;

import com.usian.behavior.feign.ArticleFeign;
import com.usian.behavior.service.ApBehaviorEntryService;
import com.usian.behavior.service.CollectionService;
import com.usian.model.article.dto.CollectionBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private ArticleFeign articleFeign;
    //收藏or取消收藏
    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //判断参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdAndEquipmentId(AppThreadLocalUtils.getUser().getId(), dto.getEquipmentId());
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(502,"行为实体不存在");
        }
        dto.setEntryId(apBehaviorEntry.getId());
        articleFeign.collection(dto);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
